<template>
  <el-card class="box-card">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm" :inline="true">
      <el-form-item prop="role_name">
        <el-input v-model="form.role_name" placeholder="角色名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">搜索</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <el-button
          type="primary"
          plain
          @click="todoCreate"
        >创建角色</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      border=""
      row-key="id"
    >
      <el-table-column
        label="角色id"
        prop="id"
        width="150px"
      />
      <el-table-column
        label="角色名称"
        prop="role_name"
      />
      <el-table-column
        label="角色标识"
        prop="identify"
      />
      <el-table-column
        label="角色描述"
        prop="description"
        width="250px"
      />
      <el-table-column
        label="添加时间"
        prop="created_at"
      />
      <el-table-column
        fixed="right"
        label="操作"
        prop="description"
      >
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handleEdit(row.id)"
          />
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handleDel(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改  权限 -->
    <el-dialog
      title="修改角色"
      :visible.sync="showDialog"
      width="35%"
      @close="btnCancel"
    >
      <!-- 公用表单 -->
      <form-role ref="fromRole" :menu-list="menuList" />
      <span slot="footer">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </span>
    </el-dialog>
    <!-- 创建角色 -->
    <el-dialog
      title="添加角色"
      :visible.sync="showAddDialog"
      width="35%"
      @close="addCancel"
    >
      <!-- 公用表单 -->
      <form-role v-if="showAddDialog" ref="fromAddRole" :menu-list="menuList" />
      <span slot="footer">
        <el-button size="small" @click="addCancel">取消</el-button>
        <el-button size="small" type="primary" @click="addOk">确定</el-button>
      </span>
    </el-dialog>
    <!-- 分页组件 -->
    <el-row
      type="flex"
      justify="center"
      align="middle"
      style="height: 60px"
    >
      <el-pagination
        background
        :total="page.total"
        :current-page="page.page"
        :page-size="page.limit"
        :page-sizes="[10, 20, 30]"
        layout="sizes,prev, pager, next"
        @current-change="changePage"
        @size-change="changeSize"
      />
    </el-row>
  </el-card>
</template>

<script>
import { getPermissionRoles, getPersonPermission, getPersonById, delPersonById, getPermissionList } from '@/api/permissions'
import FormRole from '../components/formRole'
import { filterArr } from '@/utils'
export default {
  name: 'VueAdminIndex',
  components: {
    FormRole
  },
  data() {
    return {
      form: {
        role_name: ''
      },
      menuList: [],
      rules: {
        role_name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      list: [],
      showDialog: false,
      showAddDialog: false,
      page: {
        page: 1,
        limit: 10,
        total: 0 // 总数
      }
    }
  },
  mounted() {
  },
  created() {
    this.getPermissionRoles()
    this.getPermissionList()
  },
  methods: {
    // 页码更换事件
    changePage(val) {
      // val是最新的页码数
      this.page.page = val
      this.getPermissionRoles()
    },
    // 每页数量修改事件
    changeSize(val) {
      // val是每页最新的条数
      this.page.limit = val
      this.getPermissionRoles()
    },
    async submitForm() {
      // console.log(111)
      const { data } = await getPersonPermission({ role_name: this.form.role_name, ...this.page })
      this.list = data.list
      // console.log(data)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async getPermissionRoles() {
      try {
        const { data } = await getPermissionRoles({ role_name: '', ...this.page
        })
        this.page.total = data.total
        this.list = data.list
      } catch (error) {
        console.log(error)
      }
    },
    async getPermissionList() {
      const { data } = await getPermissionList()
      this.menuList = data
    },
    async handleEdit(id) {
      this.showDialog = true
      const { data } = await getPersonById(id)
      const { authlist } = data
      this.$refs.fromRole.formData = data
      this.$refs.fromRole.selectCheck = filterArr(authlist, [])
      this.$refs.fromRole.openList = filterArr(authlist, [])
      this.$refs.fromRole.formData.authlist = this.menuList
    },
    async handleDel(id) {
      await this.$confirm('确定删除吗？').then(async() => {
        await delPersonById(id)
        // 如果不是第一页，且只有一个元素，删除成功，则需要把页码-1，重新拉取列表
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getPermissionRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.fromRole.formData = {
        description: '',
        id: 61,
        identify: '',
        role_name: '',
        authlist: []
      }
    },
    async btnOk() {
      this.$refs.fromRole.editPersonPermission()
      this.showDialog = false
      this.getPermissionRoles()
    },
    todoCreate() {
      this.showAddDialog = true
    },
    async addOk() {
      this.$refs.fromAddRole.addPermissionRole()
      this.showAddDialog = false
      this.getPermissionRoles()
      // this.getPermissionList()
    },
    addCancel() {
      this.showAddDialog = false
    }
  }
}
</script>

<style  scoped>

</style>
